/**
 * Created by kinghackers on 2016/12/6.
 */
var Comm = window.Comm = Comm || {};
Comm.time = Comm.time || {};

//公共方法
(function() {
    /*
     *  功能：判断是否为空
     *  @param value ：参数值
     *  @return 返回true表示为空
     */
    Comm.isNull = function(value) {
        if (value == undefined || value === "" || value == null) {
            return true;
        }
        if ($.trim(value) == "") {
            return true;
        }
        return false;
    };

    /**
     * 弹框显示pdf文档
     * @param filePath pdf文档相对路径
     */
    Comm.showPDF_newWimdows = function (filePath) {
        console.info(11);
        if(filePath == null || filePath == undefined || filePath.length <= 0){
            filePath = "/upload/formidable/file/ArcGIS for Desktop软件安装指导.pdf";
        }
        var src = "/libs/pdfjs/web/viewer.html?file=" + filePath;
        window.open(src,'PDF','width:100%;height:100%;top:100;left:100;');
    }
})();

//时间操作
(function() {
    Comm.time.Y = 'yyyy';
    Comm.time.YM = 'yyyy-MM';
    Comm.time.YMD = 'yyyy-MM-dd';
    Comm.time.YMDH = 'yyyy-MM-dd HH';
    Comm.time.YMDHM = 'yyyy-MM-dd HH:mm';
    Comm.time.YMDHMS = 'yyyy-MM-dd HH:mm:ss';

    /**
     * 格式化时间
     * @param {type} value
     * @returns
     */
    Comm.time.format = function(value, fmt) {
        var result;
        if (null == value) {
            return '';
        }
        if(Comm.isNull(fmt)){
            fmt = Comm.time.YMDHMS;
        }
        result = $.format.date(value, fmt);
        return result;
    }
    //获取当前年份
    Comm.time.getCurrentY = function() {
        var date = new Date();
        var year = date.getFullYear();
        return year;
    };
    //获取当前年月（yyyy-MM）
    Comm.time.getCurrentYM = function() {
        var date = new Date();
        var year = date.getFullYear();
        var month = (date.getMonth() + 1) >= 10 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1);
        var yearMon = year + "-" + month;
        return yearMon;
    };
    //获取当前年月日（yyyy-MM-dd）
    Comm.time.getCurrentYMD = function() {
        var date = new Date();
        var year = date.getFullYear();
        var month = (date.getMonth() + 1) >= 10 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1);
        var day = (date.getDate()) >= 10 ? (date.getDate()) : "0" + (date.getDate());
        var yearMonDay = year + "-" + month + "-" + day;
        return yearMonDay;
    };
    //获取当前年月日小时（yyyy-MM-dd HH）
    Comm.time.getCurrentYMDH = function() {
        var date = new Date();
        var year = date.getFullYear();
        var month = (date.getMonth() + 1) >= 10 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1);
        var day = (date.getDate()) >= 10 ? (date.getDate()) : "0" + (date.getDate());
        var hour = (date.getHours()) >= 10 ? (date.getHours()) : "0" + (date.getHours());
        var yearMonDayHour = year + "-" + month + "-" + day + " " + hour;
        return yearMonDayHour;
    };
    //根据传入的字符串时间（yyyy-MM-dd HH:mm:ss）和步长，得到相隔dayStep天的时间（yyyy-MM-dd HH）
    Comm.time.getYMDH_ByDayStep = function(hourDate, dayStep) {
        var dateTime = new Date(hourDate.replace(/-/g, "/"));
        dateTime.setDate(dateTime.getDate() + dayStep);
        var year = dateTime.getFullYear();
        var month = (dateTime.getMonth() + 1) >= 10 ? (dateTime.getMonth() + 1) : "0" + (dateTime.getMonth() + 1);
        var day = (dateTime.getDate()) >= 10 ? (dateTime.getDate()) : "0" + (dateTime.getDate());
        var hour = (dateTime.getHours()) >= 10 ? (dateTime.getHours()) : "0" + (dateTime.getHours());
        var yearMonDayHour = year + "-" + month + "-" + day + " " + hour;
        return yearMonDayHour;
    };
    //根据传入的字符串时间（yyyy-MM-dd HH:mm:ss）和步长，得到相隔dayStep天的日期（yyyy-MM-dd）
    Comm.time.getYMD_ByDayStep = function(date, dayStep) {
        var dateTime = new Date(date.replace(/-/g, "/"));
        dateTime.setDate(dateTime.getDate() + dayStep);
        var year = dateTime.getFullYear();
        var month = (dateTime.getMonth() + 1) >= 10 ? (dateTime.getMonth() + 1) : "0" + (dateTime.getMonth() + 1);
        var day = (dateTime.getDate()) >= 10 ? (dateTime.getDate()) : "0" + (dateTime.getDate());
        var yearMonDay = year + "-" + month + "-" + day;
        return yearMonDay;
    };
    //根据传入的字符串时间（yyyy-MM-dd HH:mm:ss）和日步长，得到相隔step日的时间（yyyy-MM-dd HH:mm:ss）
    Comm.time.getYMDHMS_ByDayStep = function(time, step) {
        var dateTime = new Date(time.replace(/-/g, "/"));
        dateTime.setDate(dateTime.getDate() + step);
        var year = dateTime.getFullYear();
        var month = (dateTime.getMonth() + 1) >= 10 ? (dateTime.getMonth() + 1) : "0" + (dateTime.getMonth() + 1);
        var day = (dateTime.getDate()) >= 10 ? (dateTime.getDate()) : "0" + (dateTime.getDate());
        var hour = (dateTime.getHours()) >= 10 ? (dateTime.getHours()) : "0" + (dateTime.getHours());
        var minute = (dateTime.getMinutes()) >= 10 ? (dateTime.getMinutes()) : "0" + (dateTime.getMinutes());
        var seconds = (dateTime.getSeconds()) >= 10 ? (dateTime.getSeconds()) : "0" + (dateTime.getSeconds());
        var newTime = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + seconds;
        return newTime;
    };
    //根据传入的字符串时间（yyyy-MM-dd HH:mm:ss）和小时步长，得到相隔step小时的时间（yyyy-MM-dd HH:mm:ss）
    Comm.time.getYMDHMS_ByHourStep = function(time, step) {
        var dateTime = new Date(time.replace(/-/g, "/"));
        dateTime.setHours(dateTime.getHours() + step);
        var year = dateTime.getFullYear();
        var month = (dateTime.getMonth() + 1) >= 10 ? (dateTime.getMonth() + 1) : "0" + (dateTime.getMonth() + 1);
        var day = (dateTime.getDate()) >= 10 ? (dateTime.getDate()) : "0" + (dateTime.getDate());
        var hour = (dateTime.getHours()) >= 10 ? (dateTime.getHours()) : "0" + (dateTime.getHours());
        var minute = (dateTime.getMinutes()) >= 10 ? (dateTime.getMinutes()) : "0" + (dateTime.getMinutes());
        var seconds = (dateTime.getSeconds()) >= 10 ? (dateTime.getSeconds()) : "0" + (dateTime.getSeconds());
        var newTime = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + seconds;
        return newTime;
    };
    //计算两个日期相隔多少天
    Comm.time.getDaysByDate = function(date1, date2) {
        var dateTime1 = new Date(date1.replace(/-/g, "/"));
        var dateTime2 = new Date(date2.replace(/-/g, "/"));
        return parseInt(Math.abs(dateTime1 - dateTime2) / 1000 / 60 / 60 / 24);
    };

    /**
     *获取最大天数
     */
    Comm.time.getDaysInMonth = function(year, month) {
        month = parseInt(month, 10);
        var temp = new Date(year, month, 0);
        return temp.getDate();
    };
})();

